/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.Spinner;

import id.duapuluhdua.dakidaki.data.common.DataTable;

/**
 *
 */
public class StateBundle
{
  /** Fields */
  private Bundle mState;
  private DataTable mTable;

  /**
   * Default constructor.
   *
   * @param table
   * @param outState
   */
  public StateBundle(DataTable table, Bundle outState)
  {
    mTable = table;
    mState = outState;
  }

  /**
   * Default constructor.
   *
   * @param startupState
   */
  public StateBundle(Bundle startupState)
  {
    if (startupState != null) {
      mState = (Bundle) startupState.clone();
    }
  }

  /**
   * Sets a new table by replacing the old one.
   *
   * @param table
   */
  public void setDataTable(DataTable table)
  {
    mTable = table;
  }

  /**
   * Inserts a pair of column name and string value to the saved state.
   *
   * @param columnId
   * @param value
   */
  public void set(int columnId, String value)
  {
    if ((mState != null) && (mTable != null)) {
      mState.putString(mTable.getColumnName(columnId), value);
    }
  }

  /**
   * Inserts a pair of column name and integer value to the saved state.
   *
   * @param columnId
   * @param value
   */
  public void set(int columnId, int value)
  {
    if ((mState != null) && (mTable != null)) {
      mState.putInt(mTable.getColumnName(columnId), value);
    }
  }

  /**
   * Inserts a pair of column name and string value to the saved
   * state. The string value is retrieved from an text field.
   *
   * @param columnId
   * @param textField
   */
  public void set(int columnId, EditText textField)
  {
    set(columnId, textField.getText().toString());
  }

  /**
   *
   * @param fieldId
   * @param spinner
   */
  public void setPosition(int fieldId, Spinner spinner)
  {
    int index = spinner.getSelectedItemPosition();
    if (index == Spinner.INVALID_POSITION) {
      index = 0;
    }
    set(fieldId, index);
  }

  /**
   *
   * @return
   */
  public boolean isStartupState()
  {
    return ((mState != null) && (mTable != null));
  }

  /**
   * Retrieves the string value from the saved state.
   *
   * @param columnId
   * @return Returns the string or blank if no state or table is found.
   */
  public String getString(int columnId)
  {
    if ((mState != null) && (mTable != null)) {
      mState.getString(mTable.getColumnName(columnId));
    }
    return "";
  }

  /**
   * Retrieves the integer value from the saved state.
   *
   * @param columnId
   * @return Returns the integer value or -1 if no state or table is found.
   */
  public int getInt(int columnId)
  {
    if ((mState != null) && (mTable != null)) {
      mState.getInt(mTable.getColumnName(columnId));
    }
    return -1;
  }
}
